﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace NexTrackDesktop.Media
{
    /// <summary>
    /// This class stores all song related info.
    /// </summary>
    public class Song
    {
        public string Artist { get; set; }
        public string Album { get; set; }
        public string Title { get; set; }

        [XmlIgnore]
        public TimeSpan Length { get; set; }
        
        public string SongID { get; set; }

        [XmlIgnore]
        public Uri Location { get; set; }
        
        [XmlIgnore]
        public Uri AlbumArtSource { get; set; }

        public bool HasAlbumArt { get { return AlbumArtSource != null; } }

        /// <summary>
        /// String used for serialization and deserialization of the Song class.
        /// </summary>
        [XmlElement("Location")]
        public string LocationString
        {
            get { return Location == null ? null : Location.ToString(); }
            set { Location = value == null ? null : new Uri(value); }
        }

        /// <summary>
        /// String used for serialization and deserialization of the Song class.
        /// </summary>
        [XmlElement("AlbumArtSource")]
        public string AlbumArtSourceString
        {
            get { return AlbumArtSource == null ? null : AlbumArtSource.ToString(); }
            set { AlbumArtSource = value == null ? null : new Uri(value); }
        }

        [XmlElement("Length")]
        public long LengthTicks
        {
            get { return Length.Ticks; }
            set { Length = new TimeSpan(value); }
        }

        public override bool Equals(object obj)
        {
            Song other = obj as Song;

            if (other == null)
                return false;

            return
                this.Album == other.Album &&
                this.AlbumArtSourceString == other.AlbumArtSourceString &&
                this.Artist == other.Artist &&
                this.Length == other.Length &&
                this.LocationString == other.LocationString &&
                this.SongID == other.SongID &&
                this.Title == other.Title;
        }

        public override int GetHashCode()
        {
            return this.LocationString.GetHashCode();
        }
    }
}
